﻿using Authority.Model;
using AutoMapper.DTO;
using Common.Library;
using Dapper.Library;
using Dapper.MsSql.Library;
using Sql.DataBase;
using Network.Library;
using Redis.Library;
using System;
using System.Collections.Generic;
using System.Linq;
using Tool.Model;
using Dapper.MySql.Library;

namespace Authority.Logic
{
    /// <summary>
    ///菜单操作逻辑类
    /// </summary>
    public class SysMenuServiceImpl : ISysMenuService
    {
        #region 菜单查询相关操作
        /// <summary>
        /// 获取角色可操作菜单--用于主页，左侧导航菜单
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<List<SysMenuEntity>> LoadMenusByRoles()
        {
            var resultInfo = new ResultJsonInfo<List<SysMenuEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            List<SysMenuEntity> listInfo = null;
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                string sqlStr = string.Empty;
                if (loginUser.IsSuperAdmin)
                {
                    listInfo = con.QuerySet<SysMenuEntity>()
                        .AndWhere(p => p.gParentCode != null && p.gParentCode != "" && p.bIsDeleted == false && p.bIsEnabled == true)
                        .OrderByAsc("iSort")
                        .Multiple();
                }
                else
                {
                    var sRoleCodes = loginUser.RoleCodes.Split(',').ToList();
                    var gCodes = con.QuerySet<SysRoleMenuEntity>()
                        .AndWhere(p => sRoleCodes.Contains(p.gRoleCode))
                        .Multiple().Select(p => p.gMenuCode)
                        .ToList();
                    if (gCodes.Count>0)
                    {
                        listInfo = con.QuerySet<SysMenuEntity>()
                                              .AndWhere(p => p.gParentCode != null 
                                                      && p.gParentCode != "" 
                                                      && p.bIsDeleted == false 
                                                      && p.bIsEnabled == true
                                                      && gCodes.Contains(p.gCode))
                                              .OrderByAsc("iSort")
                                              .Multiple();
                    }
                }
            }
            if (listInfo.Count > 0)
            {
                 //可操作菜单，放redis中
                 RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb)
                    .HashSet<List<SysMenuEntity>>(ConstRedisKey.RedisMenusCode, loginUser.LoginName, listInfo);

                resultInfo.Code = ActionCodes.Success;
                resultInfo.Data = listInfo;
                resultInfo.Msg = "获取成功！";
            }
            else
            {
                resultInfo.Msg = "获取失败！";
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <param name="ParentCode"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<MenuInfo>> LoadSubMenus(string ParentCode)
        {
            var resultInfo = new ResultJsonInfo<List<MenuInfo>>();

            List<MenuInfo> listInfo = new List<MenuInfo>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {

                //可操作菜单，redis中获取
                var menus = RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb)
                    .HashGet<List<SysMenuEntity>>(ConstRedisKey.RedisMenusCode, loginUser.LoginName);

                //循环整个菜单
                menus.ForEach(item =>
                {
                    if (item.gParentCode == ParentCode)
                    {
                        var menu = new MenuInfo()
                        {
                            Code = item.gCode,
                            Name = item.sName,
                            Url = item.sUrl,
                            Target = item.sTarget,
                            IconFont = item.sIconFont,
                            Sort = item.iSort,
                            IsSpread = Convert.ToBoolean(item.bIsSpread),
                            Children = new List<MenuInfo>()
                        };

                        var subMenus = menus.Where(p => p.gParentCode == item.gCode).Select(t => new MenuInfo()
                        {
                            Code = t.gCode,
                            Name = t.sName,
                            Url = t.sUrl,
                            Target = t.sTarget,
                            IconFont = t.sIconFont,
                            Sort = t.iSort,
                            IsSpread = Convert.ToBoolean(t.bIsSpread),
                            Children = new List<MenuInfo>()

                        }).ToList();

                        if (subMenus != null && subMenus.Count > 0)
                        {
                            menu.Children.AddRange(subMenus);
                        }

                        listInfo.Add(menu);
                    }
                });
            }
            if (listInfo.Count > 0)
            {
                resultInfo.Code = ActionCodes.Success;
                resultInfo.Data = listInfo;
                resultInfo.Msg = "获取成功！";
            }
            else
            {
                resultInfo.Msg = "获取失败！";
            }
            return resultInfo;
        }

        /// <summary>
        /// 根据Name获取菜单信息
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<List<MenusListResponse>> LoadMenusList(string sName)
        {
            var resultInfo = new ResultJsonInfo<List<MenusListResponse>>();
            List<SysMenuEntity> listInfo = null;

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                if (sName.IsNotNullOrEmpty())
                {
                    //listInfo = con.QuerySql<SysMenuEntity>().BySql(DataCommandManager.GetSQLCommand("LoadMenusByName", "SysMenu"), new { sName = sName }).Multiple();
                    var allInfo = con.QuerySet<SysMenuEntity>().AndWhere(p => p.bIsDeleted == false).OrderByAsc("iSort").Multiple();
                    var rootInfo = allInfo.FindAll(p => p.sName.Contains(sName));
                    if (rootInfo.Count > 0)
                    {
                        listInfo.AddRange(rootInfo);
                        listInfo.AddRange(allInfo.GetChildList(rootInfo));
                        listInfo.OrderBy(p => p.iSort);
                    }
                }
                else
                {
                    listInfo = con.QuerySet<SysMenuEntity>().AndWhere(p => p.iLevel != (int)MenuLevel.First && p.bIsDeleted == false).OrderByAsc("iSort").Multiple();
                }
            }
            if (listInfo.Count > 0)
            {
                var menusList = listInfo.MapToList<MenusListResponse>();
                foreach (var item in menusList)
                {
                    //NET算术运算溢出问题
                    item.bHaveElder = listInfo.Exists(p => p.iSort < item.iSort && p.iLevel == item.iLevel && p.gCode != item.gCode && p.gParentCode == item.gParentCode);
                    item.bHaveYounger = listInfo.Exists(p => p.iSort > item.iSort && p.iLevel == item.iLevel && p.gCode != item.gCode && p.gParentCode == item.gParentCode);
                }
                resultInfo.Code = ActionCodes.Success;
                resultInfo.Data = menusList;
                resultInfo.Count = menusList.Count;
                resultInfo.Msg = "获取成功！";
            }
            else
            {
                resultInfo.Msg = "获取失败！";
            }
            return resultInfo;
        }
        #endregion

        #region 菜单编辑相关操作
        /// <summary>
        /// 新增根节点信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> AddRoot(MenusRootRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();

            var sysMenu = inputInfo.MapTo<SysMenuEntity>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = 0;
                //获取所有菜单信息
                var sysMenuResult = con.QuerySet<SysMenuEntity>().WithNoLock().Multiple();

                int maxSort = sysMenuResult.Select(p => p.iSort).Max();
                var sysMenuInfo = sysMenuResult.Find(p => p.gParentCode == null); ;

                sysMenu.gCode = GuidHelper.GetGuid();
                sysMenu.iLevel = (int)MenuLevel.Second;
                sysMenu.gParentCode = sysMenuInfo.gCode;
                sysMenu.iSort = maxSort + 1;

                result = con.CommandSet<SysMenuEntity>()
                       .IfNotExists(p => p.sName == sysMenu.sName)
                       .Insert(sysMenu);

                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "新增根节点信息成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败,请检查菜单名称是否重复！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 新增子节点信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> AddChild(MenusChildRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();

            var sysMenu = inputInfo.MapTo<SysMenuEntity>();
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = 0;

                var sysMenuResult = con.QuerySet<SysMenuEntity>().WithNoLock().Multiple();
                sysMenu.gCode = GuidHelper.GetGuid();

                var sysMenuInfos = sysMenuResult.FindAll(p => p.iSort >= sysMenu.iSort);
                foreach (var item in sysMenuInfos)
                {
                    item.iSort = short.Parse((item.iSort + short.Parse("1")).ToString());
                    con.CommandSet<SysMenuEntity>().Update(item);
                }

                result = con.CommandSet<SysMenuEntity>()
                       .IfNotExists(p => p.sName == sysMenu.sName)
                       .Insert(sysMenu);

                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "新增子节点信息成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败,请检查菜单名称是否重复！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Modify(MenusModifyRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();
            var sysMenu = inputInfo.MapTo<SysMenuEntity>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = 0;
                con.Transaction(tran=> {

                    var sysMenuInfo = tran.QuerySet<SysMenuEntity>().AndWhere(p => p.gCode == sysMenu.gCode).Single();
                    if (sysMenuInfo != null)
                    {
                        sysMenuInfo.sName = sysMenu.sName;
                        sysMenuInfo.sIconFont = sysMenu.sIconFont;
                        sysMenuInfo.sUrl = sysMenu.sUrl;
                        sysMenuInfo.bIsEnabled = sysMenu.bIsEnabled;

                        result = tran.CommandSet<SysMenuEntity>()
                            .IfNotExists(p => p.gCode != inputInfo.gCode && p.sName == inputInfo.sName)
                            .Update(sysMenuInfo);

                        if (result > 0)
                        {
                            //修改时，如果是禁用菜单。。需要将它的子菜单一起禁用
                            if (sysMenu.bIsEnabled == false)
                            {
                                //var listInfo = con.QuerySql<SysMenuEntity>().BySql(DataCommandManager.GetSQLCommand("LoadMenusByName", "SysMenu"), new { sName = sysMenu.sName }).Multiple();

                                var listInfo = new List<SysMenuEntity>();
                                var allInfo = tran.QuerySet<SysMenuEntity>().AndWhere(p => p.bIsDeleted == false).Multiple();
                                var rootInfo = allInfo.FindAll(p => p.sName.Contains(sysMenu.sName));
                                if (rootInfo.Count > 0)
                                {
                                    listInfo.AddRange(rootInfo);
                                    listInfo.AddRange(allInfo.GetChildList(rootInfo));
                                }
                                foreach (var item in listInfo)
                                {
                                    item.bIsEnabled = false;
                                    tran.CommandSet<SysMenuEntity>().Update(item);
                                }
                            }
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "无对应菜单信息！";
                    }
                });
                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "修改菜单信息成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败,请检查菜单名称是否重复！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Remove(List<string> gCodeList)
        {
            var resultInfo = new ResultJsonInfo<int>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
          //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                con.Transaction(tran=> {

                    var result = 0;
                    if (gCodeList.Count >= 0)
                    {
                        //var listCode = con.QuerySql<string>().BySql(DataCommandManager.GetSQLCommand("LoadMenusByCodes", "SysMenu"),
                        //       new
                        //       {  gCodeList = gCodeList
                        //       }).Multiple();

                        var listInfo = new List<SysMenuEntity>();
                        var allInfo = tran.QuerySet<SysMenuEntity>().AndWhere(p => p.bIsDeleted == false).Multiple();
                        var rootInfo = allInfo.FindAll(p => gCodeList.Contains(p.gCode));
                        if (rootInfo.Count > 0)
                        {
                            listInfo.AddRange(rootInfo);
                            listInfo.AddRange(allInfo.GetChildList(rootInfo));
                        }
                        var listCode = listInfo.Select(p => p.gCode).ToList();

                        //查看是否被角色使用，如果没使用，可以删除
                        var roleMenuInfo = tran.QuerySet<SysRoleMenuEntity>().AndWhere(p => listCode.Contains(p.gMenuCode)).Multiple();
                        if (roleMenuInfo.Count == 0)
                        {
                            //逻辑删除菜单
                            result= tran.CommandSet<SysMenuEntity>().AndWhere(p=> listCode.Contains(p.gCode))
                                        .UpdateByExp(a =>
                                            new SysMenuEntity
                                            {
                                                bIsDeleted = true
                                            });

                            //物理删除菜单使用的按钮
                            tran.CommandSet<SysMenuButtonEntity>().AndWhere(p => listCode.Contains(p.gMenuCode)).Delete();

                            if (result > 0)
                            {
                                resultInfo.Code = ActionCodes.Success;
                                resultInfo.Data = result;
                                resultInfo.Msg = "操作成功！";
                            }
                            else
                            {
                                resultInfo.Msg = "操作失败！";
                            }
                        }
                        else
                        {
                            resultInfo.Msg = "选中菜单已被角色已使用，无法删除！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "id为空，操作失败！";
                    }
                });
            }
            return resultInfo;
        }
        #endregion

        #region 移动顺序操作
        /// <summary>
        /// 移动顺序操作
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Move(MenusMoveRequest inputInfo)
        {

            var result = new ResultJsonInfo<int>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    //（1）所有功能
                    var adminFuns = tran.QuerySet<SysMenuEntity>().AndWhere(p => p.bIsDeleted == false).OrderByAsc("iSort").Multiple();

                    //（2）当前功能 
                    SysMenuEntity currAdminFun = adminFuns.Find(p => p.gCode == inputInfo.Code);

                    //（3）当前功能兄弟节点
                    List<SysMenuEntity> brothersAdminFun = adminFuns.FindAll(p => p.gParentCode == currAdminFun.gParentCode);

                    SysMenuEntity broAdminFun = null;

                    foreach (SysMenuEntity item in brothersAdminFun)
                    {
                        if (inputInfo.Flag == 1)//上移
                        {
                            if (item.iSort < currAdminFun.iSort) { broAdminFun = item; }
                            if (item.iSort > currAdminFun.iSort) { break; } //找到上一个兄弟节点
                        }
                        else //下移
                        {
                            if (item.iSort > currAdminFun.iSort) { broAdminFun = item; break; } //找到下一个兄弟节点
                        }
                    }

                    List<SysMenuEntity> currFuns = new List<SysMenuEntity>();
                    currFuns.Add(currAdminFun);
                    GetMoveNode(adminFuns, currAdminFun.gCode, currFuns);

                    List<SysMenuEntity> moveFuns = new List<SysMenuEntity>();
                    moveFuns.Add(broAdminFun);
                    GetMoveNode(adminFuns, broAdminFun.gCode, moveFuns);

                    //上移
                    if (inputInfo.Flag == 1)
                    {
                        foreach (SysMenuEntity item in currFuns)
                        {
                            item.iSort = short.Parse((item.iSort - short.Parse(moveFuns.Count.ToString())).ToString());
                            tran.CommandSet<SysMenuEntity>().Update(item);
                        }

                        foreach (SysMenuEntity item in moveFuns)
                        {
                            item.iSort = short.Parse((item.iSort + short.Parse(currFuns.Count.ToString())).ToString());
                            tran.CommandSet<SysMenuEntity>().Update(item);
                        }
                    }
                    else
                    {
                        foreach (SysMenuEntity item in currFuns)
                        {
                            item.iSort = short.Parse((item.iSort + short.Parse(moveFuns.Count.ToString())).ToString());
                            tran.CommandSet<SysMenuEntity>().Update(item);
                        }
                        foreach (SysMenuEntity item in moveFuns)
                        {
                            item.iSort = short.Parse((item.iSort - short.Parse(currFuns.Count.ToString())).ToString());
                            tran.CommandSet<SysMenuEntity>().Update(item);
                        }
                    }
                });
                result.Code = ActionCodes.Success;
                result.Data = 1;
            }
            return result;
        }

        /// <summary>
        /// 上下一定子功能（递归）
        /// </summary>
        /// <param name="adminFuns"></param>
        /// <param name="funId"></param>
        /// <param name="move"></param>
        private void GetMoveNode(List<SysMenuEntity> adminFuns, string funId, List<SysMenuEntity> move)
        {
            List<SysMenuEntity> borgs = adminFuns.FindAll(r => r.gParentCode == funId);
            foreach (SysMenuEntity item in borgs)
            {
                if (item.gParentCode == funId)
                {
                    move.Add(item);
                    List<SysMenuEntity> tempFuns = adminFuns.FindAll(r => r.gParentCode == item.gCode);
                    if (tempFuns != null && tempFuns.Count > 0)
                    {
                        GetMoveNode(adminFuns, item.gCode, move);
                    }
                }
            }
        }
        #endregion

        #region 菜单按钮相关操作

        /// <summary>
        /// 获取按钮信息
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<MenuButtonsListResponse> LoadButtons(string menuCode)
        {

            var resultInfo = new ResultJsonInfo<MenuButtonsListResponse>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                if (menuCode.IsNotNullOrEmpty())
                {
                    var listInfo = con.QuerySet<SysButtonEntity>().AndWhere(p=>p.bIsDeleted==false).OrderByAsc("iSort").Multiple();
                    if (listInfo.Count > 0)
                    {
                        var menuButtonListInfo = con.QuerySet<SysMenuButtonEntity>().AndWhere(P => P.gMenuCode == menuCode).OrderByAsc("iSort").Multiple();
                        resultInfo.Data = new MenuButtonsListResponse();
                        resultInfo.Data.AllButtonList = listInfo;
                        resultInfo.Data.SelectedButtonList = menuButtonListInfo.Select(p => p.gButtonCode).ToList();
                        resultInfo.Code = ActionCodes.Success;
                        resultInfo.Msg = "获取成功！";
                    }
                    else
                    {
                        resultInfo.Msg = "获取失败！";
                    }
                }
                else
                {
                    resultInfo.Msg = "菜单唯一编码不嫩为空！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存选中按钮
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> SaveButtons(MenuButtonsSaveRequest inputInfo)
        {
            var result = new ResultJsonInfo<int>();

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {

                var buttonCodes = inputInfo.buttonCodeList.ToArray();

                //获取该菜单原有的所有菜单按钮关系信息
                var menuButtonListInfo = con.QuerySet<SysMenuButtonEntity>()
                                .AndWhere(p => p.gMenuCode == inputInfo.menuCode)
                                .Multiple();

                //1、判断删除掉的按钮是否已经被角色使用。
                #region 判断删除掉的按钮是否已经被角色使用，如果被使用了，就返回失败信息；如果没被使用，就删除掉。

                //找出需要删除的菜单-按钮关系表
                var needRemoveMenuButtonListInfo =  menuButtonListInfo.FindAll(p => !buttonCodes.Contains(p.gButtonCode))
                    .Select(p=>p.gCode)
                    .ToArray();

                if (needRemoveMenuButtonListInfo.Length> 0)
                {
                    var sysRoleButtonInfo = con.QuerySet<SysRoleButtonEntity>().AndWhere(p => p.gMenuButtonCode.WhereIn(needRemoveMenuButtonListInfo)).Exists();
                    //2、如果被使用，可以做下面操作。
                    if (sysRoleButtonInfo)
                    {
                        result.Msg = "已有按钮被角色使用，无法删除！";
                        return result;
                    }
                    else
                    {
                        //删除掉不用的按钮
                        con.CommandSet<SysMenuButtonEntity>().AndWhere(p => p.gCode.WhereIn(needRemoveMenuButtonListInfo)).Delete();
                    }
                }
                #endregion

                #region 做按钮添加操作

                var newButtonCodes = buttonCodes.ToList().FindAll(p => !menuButtonListInfo.Select(menu=> menu.gButtonCode).Contains(p)).ToArray();
                if (newButtonCodes.Length>0)
                {
                    List<SysMenuButtonEntity> menuButtons = new List<SysMenuButtonEntity>();
                    SysMenuButtonEntity menuButton = null;
                    int index = 0;

                    foreach (var item in newButtonCodes)
                    {
                        menuButton = new SysMenuButtonEntity();
                        menuButton.gCode = GuidHelper.GetGuid();
                        menuButton.gButtonCode = item;
                        menuButton.gMenuCode = inputInfo.menuCode;
                        menuButton.iSort = index++;
                        menuButtons.Add(menuButton);
                    }
                    con.CommandSet<SysMenuButtonEntity>().BatchInsert(menuButtons);
                }

                result.Code = ActionCodes.Success;
                result.Data = 1;
                result.Msg = "操作成功！";
                #endregion

            }
            return result;
        }
        #endregion

        #region 角色授权相关

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<List<RoleMenusListResponse>> LoadMenusListInfoByRoleCode(string gRoleCode)
        {
            var resultInfo = new ResultJsonInfo<List<RoleMenusListResponse>>();

            var menusList = new List<RoleMenusListResponse>();
            List<SysMenuEntity> listInfo = null;
            List<SysRoleMenuEntity> sysRoleMenu = null;

            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                listInfo = con.QuerySet<SysMenuEntity>()
                    .AndWhere(p => p.iLevel != (int)MenuLevel.First && p.bIsDeleted == false && p.bIsEnabled == true)
                    .OrderByAsc("iSort").Multiple();

                sysRoleMenu = con.QuerySet<SysRoleMenuEntity>().AndWhere(p => p.gRoleCode == gRoleCode).Multiple();

                menusList = listInfo.MapToList<RoleMenusListResponse>();

                foreach (var item in menusList)
                {
                    if (sysRoleMenu.Exists(p => p.gMenuCode == item.gCode))
                    {
                        item.bIsSelected = true;
                    }
                }
            }
            if (listInfo.Count > 0)
            {
                resultInfo.Code = ActionCodes.Success;
                resultInfo.Data = menusList;
                resultInfo.Count = menusList.Count;
                resultInfo.Msg = "获取成功！";
            }
            else
            {
                resultInfo.Msg = "获取失败！";
            }
            return resultInfo;
        }


        #endregion

    }
}
